package org.eenie.kdassistant.net.kdy_api;

import com.alibaba.fastjson.JSON;

import org.eenie.kdassistant.App;
import org.eenie.kdassistant.net.AssistantApi;
import org.eenie.kdassistant.net.ParamsAnalog;
import org.eenie.kdassistant.net.common.CommonApi;
import org.eenie.kdassistant.utils.kdy_encode.KdyMD5Util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Eenie on 2017/8/11.
 * Email: eeniework@gmail.com
 * Des:
 */

public class KdyApi implements CommonApi {
    private KdyService mKdyService;
    private SimpleDateFormat mSimpleDateFormat;
    private ParamsAnalog mParamsAnalog;
    private String onlyCode;
    private AssistantApi mAssistantApi;

    public KdyApi(App app, OkHttpClient client, ParamsAnalog paramsAnalog, AssistantApi assistantApi) {
        mAssistantApi = assistantApi;
        mParamsAnalog = paramsAnalog;

        mKdyService = new Retrofit.Builder()
                .baseUrl("http://dts.kuaidihelp.com")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build()
                .create(KdyService.class);

        onlyCode = KdyMD5Util.getOnlyCode(app);

        mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }


    @Override
    public Observable<ResponseBody> addAcceptExpress(String express) {
        Map<String, Object> params = new HashMap<>();
        params.put("sname", "scan.to.e3.v2");
        params.put("appVersion", "111");
        params.put("wayBillType", "5");
        params.put("dev_id", onlyCode);
        params.put("dev_imei", mParamsAnalog.getDevice_imei());
        params.put("signPics", new Object());
        params.put("sendSms", "0");
        params.put("pname", "androids");

        List dataList = new ArrayList();

        Map<String, Object> data = new HashMap<>();


        Map<String, Object> location = new HashMap<>();

        location.put("latitude", mParamsAnalog.getDevice_lat());
        location.put("longitude", mParamsAnalog.getDevice_lng());

        data.put("waybillNo", express);
        data.put("scan_time", mSimpleDateFormat.format(System.currentTimeMillis()));
        data.put("latitude", mParamsAnalog.getDevice_lat());
        data.put("longitude", mParamsAnalog.getDevice_lng());
        data.put("forward_station", "");
        data.put("location", location);
        dataList.add(data);
        params.put("wayBillDatas", dataList);

//        String content = "{\"sname\":\"scan.to.e3.v2\",\"appVersion\":111,\"wayBillType\":5,\"dev_id\":\"K50163979957976\",\"dev_imei\":\"860162035890117\",\"wayBillDatas\":[{\"waybillNo\":\"668156168238\",\"scan_time\":\"2017-08-11 15:16:51\",\"latitude\":\"31.202237\",\"longitude\":\"121.471749\",\"forward_station\":\"\",\"location\":{\"latitude\":\"31.202237\",\"longitude\":\"121.471749\"}}],\"signPics\":{},\"sendSms\":\"0\",\"pname\":\"androids\"}";
        String content = JSON.toJSONString(params);

        FormBody.Builder builder = new FormBody.Builder();
        builder.add("content", content);
        builder.add("token", KdyMD5Util.toMD5(content+ "bac500a42230c8d7d1820f1f1fa9b578"));
        return mKdyService.ReqData("session_id=" + mParamsAnalog.getKdy_sessionId(), builder.build());
    }

    @Override
    public Observable<ResponseBody> addSignExpress(String express) {

        Map<String, Object> params = new HashMap<>();
        params.put("sname", "scan.to.e3.v2");
        params.put("appVersion", "111");
        params.put("wayBillType", "3");
        params.put("dev_id", onlyCode);
        params.put("dev_imei", mParamsAnalog.getDevice_imei());
        params.put("signPics", new Object());
        params.put("sendSms", "0");
        params.put("pname", "androids");

        List dataList = new ArrayList();

        Map<String, Object> data = new HashMap<>();


        data.put("waybillNo", express);
        data.put("scan_time", mSimpleDateFormat.format(System.currentTimeMillis()));
        data.put("latitude", mParamsAnalog.getDevice_lat());
        data.put("longitude", mParamsAnalog.getDevice_lng());
        data.put("signType", "本人签收");

        dataList.add(data);
        params.put("wayBillDatas", dataList);



        String content = JSON.toJSONString(params);

        FormBody.Builder builder = new FormBody.Builder();
        builder.add("content", content);
        builder.add("token", KdyMD5Util.toMD5(content+ "bac500a42230c8d7d1820f1f1fa9b578"));

        return mKdyService.ReqData("session_id=" + mParamsAnalog.getKdy_sessionId(), builder.build());
    }

    @Override
    public Observable<ResponseBody> fetchCom(String express) {
        return mAssistantApi.fetchCom(express);
    }

    public Observable<ResponseBody> login(String account,String pwd) {
        long t = System.currentTimeMillis();
        StringBuffer sb = new StringBuffer();
        sb.append(t)
                .append("4accd1296e8f514627a411e4e2fbfc5f")
                .append("/v1/" + "Wduser/login".replace(".", "/"))
                .append("10002");
        FormBody.Builder builder = new FormBody.Builder();
        Map<String, Object> params = new HashMap<>();
        params.put("user_name", account);
        params.put("user_pwd", pwd);
        builder.add("app_id", "10002");
        builder.add("ts", String.valueOf(t));
        builder.add("sign", KdyMD5Util.toMD5(sb.toString()));
        builder.add("data", JSON.toJSONString(params));

        return mKdyService.login("", builder.build())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }


//    public Observable<ResponseBody> getCount() {
//        FormBody.Builder builder = FormBodyBuilderFactory.create(0, null, extMap, "0104", "90d46ed77a284116816082a6bb389678");
//        return mXingZheService.ReqData("0104", builder.build());
//    }


}
